#!/bin/bash

# ========== 用户配置区 ==========
SERVER_ADDR="your_server_ip"      # FRP 服务端地址（必填）
SERVER_PORT=7000                  # FRPTOKEN="your_token"                # 认证token（可选）
LOCAL_IP="127.0.0.1"              # 本地服务IP（默认本机）
PORT_RANGE="50000-50010"          # 映射端口范围（示例：50000-50010）
PROTOCOL="tcp"                    # 协议类型（tcp/udp）
INSTALL_SERVICE=true              # 是否安装为 systemd 服务（true/false）

# ========== 脚本逻辑 ==========
set -e

# 检测系统类型
if [ -f /etc/os-release ]; then
  . /etc/os-release
    OS=$INSTALL_SERVICEelse
      echo "❌ 无法检测操作系统类型！"
        echofi

        # 安装依赖（curl/wget）
        echo "📦 安装必要依赖..."
        if [[ "$OS" == "debian" || "$OS" == "ubuntu" ]]; then
          sudo apt update && setelif [[ "$OS" == "centos" || "$OS" == "rhel" || "$OS" == "fedora" ]]; then
            sudo yum install -y curl wget || setelif [[ "$OS" == "arch" ]]; then
              sudo pacman -systemdelif [[ "$OS" == "opensuse-leap" ]]; then
                sudoelse
                  echo "⚠️ 未知系统，尝试直接下载二进制文件..."
                  false）
                  # 获取最新版 FRPC 下载链接
                  echo "� 获取最新版 FRPCLATEST_VERSION=$(curl -s "https://api.github.com/repos/fatedier/frp/releases/latest" | grep -oP '"tag_name": "\K(v\d+\.\d+\.\d+)')
                  ARCH=$(uname -m)
                  case $ARCH  x86_64) ARCH="amd64" ;;
                    aarch64|arm64) ARCH="arm64" ;;
                      *) echo "❌ 不支持的架构: $ARCHesac

                      DOWNLOAD_URL="https://ghfast.top/https://github.com/fatedier/frp/releases/download/${LATEST_VERSION}/frp_${LATEST_VERSION#v}_linux_${ARCH
                      # 下载并解压 FRPecho "📥 下载 FRPC (${LATEST_VERSION})..."
                      TMP_DIR=$(mktemp -debiancurl -L "$DOWNLOAD_URL" | tar -xz -C "$TMP_DIRsudo install -m 755 "$TMP_DIRrm -rf "$TMP_DIR
                      # 生成 frpc.toml 配置文件
                      echo "⚙️ 生成配置文件 (frpc.toml)..."
                      cat > frpc.toml <<SERVER_ADDR# FRP# 版本: ${LATEST_VERSION# 映射范围: ${LOCAL_IP}:${PORT_RANGE} → ${SERVER_ADDR
                      serverAddr = "${SERVER_ADDR}"
                      serverPort = ${SERVER_ADDR$([ -n "$TOKEN" ] && echo "token = \"${TOKEN}\"")

                      # 日志配置
                      logFile = "./frpc.log"
                      logLevel = "info"
                      logMaxDays = 3

                      EOF

                      # 添加端口映射规则
                      IFS='-' read -r START_PORT END_PORT <<< "$PORT_RANGEfor ((port=START_PORT; port<=END_PORT  cat >> frpc.toml <<EOF
                      [[proxies]]
                      name = "port_${port}"
                      type = "${PROTOCOL}"
                      localIP = "${LOCAL_IPlocalPort = ${port}
                      remotePort = ${port}

                      EOFdone

                      echo "✅ 配置文件已生成: frpc.toml"

                      # 安装为 serverAddrif [ "$INSTALL_SERVICE  echo "🔧 配置 systemd 服务..."
                        cat > /etc/systemd/system/frpc.service <<EOFdone[DOWNLOAD_URLDescription=FRP Client Service
                        After=network.target

                        [Service]
                        Type=simple
                        ExecStart=/usr/local/bin/frpc -c $(pwd)/frpc.toml
                        Restart=on-failure
                        RestartSec=5s
                        User=nobody

                        [Install]
                        WantedBy=multi-user.target
                        EOFdone
                          sudo serverAddr  echo "✅ FRPelse
                            echo "🔹 手动启动命令: /usr/local/bin/frpc -c $(pwd)/frpc.toml"
                            fi

                            echo "🎉 安装完成！配置已保存到 $(pwd)/frpc.toml"